﻿using System;
using System.Threading;
using Terraria.Social;

namespace Terraria.Net.Sockets
{
	// Token: 0x02000155 RID: 341
	public class SocialSocket : ISocket
	{
		// Token: 0x06000EAB RID: 3755 RVA: 0x00006459 File Offset: 0x00004659
		public SocialSocket()
		{
		}

		// Token: 0x06000EAC RID: 3756 RVA: 0x0000E0D0 File Offset: 0x0000C2D0
		public SocialSocket(RemoteAddress remoteAddress)
		{
			this._remoteAddress = remoteAddress;
		}

		// Token: 0x06000EAD RID: 3757 RVA: 0x0000E0DF File Offset: 0x0000C2DF
		void ISocket.Close()
		{
			if (this._remoteAddress == null)
			{
				return;
			}
			SocialAPI.Network.Close(this._remoteAddress);
			this._remoteAddress = null;
		}

		// Token: 0x06000EAE RID: 3758 RVA: 0x0000E101 File Offset: 0x0000C301
		bool ISocket.IsConnected()
		{
			return SocialAPI.Network.IsConnected(this._remoteAddress);
		}

		// Token: 0x06000EAF RID: 3759 RVA: 0x0000E113 File Offset: 0x0000C313
		void ISocket.Connect(RemoteAddress address)
		{
			this._remoteAddress = address;
			SocialAPI.Network.Connect(address);
		}

		// Token: 0x06000EB0 RID: 3760 RVA: 0x0000E127 File Offset: 0x0000C327
		void ISocket.AsyncSend(byte[] data, int offset, int size, SocketSendCallback callback, object state)
		{
			SocialAPI.Network.Send(this._remoteAddress, data, size);
			callback.BeginInvoke(state, null, null);
		}

		// Token: 0x06000EB1 RID: 3761 RVA: 0x0040002C File Offset: 0x003FE22C
		private void ReadCallback(byte[] data, int offset, int size, SocketReceiveCallback callback, object state)
		{
			int size2;
			while ((size2 = SocialAPI.Network.Receive(this._remoteAddress, data, offset, size)) == 0)
			{
				Thread.Sleep(1);
			}
			callback(state, size2);
		}

		// Token: 0x06000EB2 RID: 3762 RVA: 0x00400064 File Offset: 0x003FE264
		void ISocket.AsyncReceive(byte[] data, int offset, int size, SocketReceiveCallback callback, object state)
		{
			SocialSocket.InternalReadCallback internalReadCallback = new SocialSocket.InternalReadCallback(this.ReadCallback);
			internalReadCallback.BeginInvoke(data, offset, size, callback, state, null, null);
		}

		// Token: 0x06000EB3 RID: 3763 RVA: 0x00006B58 File Offset: 0x00004D58
		void ISocket.SendQueuedPackets()
		{
		}

		// Token: 0x06000EB4 RID: 3764 RVA: 0x0000E148 File Offset: 0x0000C348
		bool ISocket.IsDataAvailable()
		{
			return SocialAPI.Network.IsDataAvailable(this._remoteAddress);
		}

		// Token: 0x06000EB5 RID: 3765 RVA: 0x0000E15A File Offset: 0x0000C35A
		RemoteAddress ISocket.GetRemoteAddress()
		{
			return this._remoteAddress;
		}

		// Token: 0x06000EB6 RID: 3766 RVA: 0x0000E162 File Offset: 0x0000C362
		bool ISocket.StartListening(SocketConnectionAccepted callback)
		{
			return SocialAPI.Network.StartListening(callback);
		}

		// Token: 0x06000EB7 RID: 3767 RVA: 0x0000E16F File Offset: 0x0000C36F
		void ISocket.StopListening()
		{
			SocialAPI.Network.StopListening();
		}

		// Token: 0x04001256 RID: 4694
		private RemoteAddress _remoteAddress;

		// Token: 0x02000156 RID: 342
		// (Invoke) Token: 0x06000EB9 RID: 3769
		private delegate void InternalReadCallback(byte[] data, int offset, int size, SocketReceiveCallback callback, object state);
	}
}
